package com.yingran.common

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.support.v7.app.AppCompatActivity

open class CommonActivity : AppCompatActivity() {
    var handler: Handler? = null
    lateinit var context: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context = this@CommonActivity
    }

    override fun onDestroy() {
        if (handler != null) {
            handler!!.removeCallbacksAndMessages(null)
        }
        super.onDestroy()
    }


    fun post(runnable: Runnable) {
        if (handler == null) {
            handler = Handler(Looper.getMainLooper())
        }
        handler!!.post { runnable.run() }
    }

    fun postDelay(runnable: Runnable, delay: Long) {
        if (handler == null) {
            handler = Handler(Looper.getMainLooper())
        }
        handler!!.postDelayed({ runnable.run() }, delay)
    }

    override fun onBackPressed() {
        if (handler != null) {
            handler!!.removeCallbacksAndMessages(null)
        }
        super.onBackPressed()
    }
}